package com.example.ywj.toutiao.fragments;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.material.tabs.TabLayout;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.example.ywj.toutiao.R;
import com.example.ywj.toutiao.bean.Title;
import com.example.ywj.toutiao.utils.Constant;
import com.example.ywj.toutiao.utils.MyFragmentPageAdapter5;
import com.example.ywj.toutiao.utils.ResponseApi;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class VideoFragment extends Fragment {
    private static final String TAG = "VideoFragment";
    public View view;
    public ViewPager viewPager_video;
    public TabLayout tabLayout_video;
    public String baseUrl = Constant.BASEURL;
    public List<String> strTitle = new ArrayList<>();
    public MyFragmentPageAdapter5 myFragmentPageAdapter5;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if(view == null) {
            view = inflater.inflate(R.layout.video_fragment, null);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Window window = getActivity().getWindow();
                window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.setStatusBarColor(Color.TRANSPARENT);
            }
        }
        initView();
        getData();
        return view;
    }
    public void initView(){
        viewPager_video = view.findViewById(R.id.viewpager_video);
        tabLayout_video = view.findViewById(R.id.tabLayout_video);
    }
    public void getData(){
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(baseUrl)
                .build();
        ResponseApi responseApi = retrofit.create(ResponseApi.class);
        Call<ResponseBody> call = responseApi.getDatass();
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                String json = null;
                try {
                    json = response.body().string();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Log.i(TAG, "onResponse: "+json);
                JsonObject obj = (JsonObject) new JsonParser().parse(json);
                Integer code = obj.get("statusCode").getAsInt();
                if(code ==200 &&obj.has("data")){
                    JsonArray data = obj.getAsJsonArray("data");
                    Gson gson = new Gson();
                    List<Title> list = gson.fromJson(data,new TypeToken<ArrayList<Title>>(){}.getType());
                    for(int i = 0;i<list.size();i++){
                        strTitle.add(list.get(i).getTitle());
                    }
                    addTabToTabLayout(strTitle);
                    tabLayout_video.setupWithViewPager(viewPager_video);
                    myFragmentPageAdapter5 = new MyFragmentPageAdapter5(getActivity().getSupportFragmentManager(),strTitle);
                    viewPager_video.setAdapter(myFragmentPageAdapter5);
                    viewPager_video.setOffscreenPageLimit(strTitle.size());
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.i(TAG, "onFailure: 请求出错了");
            }
        });
    }
    private void addTabToTabLayout(List<String> list) {
        for (int i = 0; i < list.size(); i++) {
            tabLayout_video.addTab(tabLayout_video.newTab().setText(list.get(i)));
        }
    }
}
